<?
class Trackmodel extends CI_Model {
	var $err_msg=false;

	function __construct()
	{
		parent::__construct();
	}

	function get_track_url($track_id){
		$this->db->where('track_id',$track_id);
		$q = $this->db->get('tracks');
		$result=false;
		if($q->num_rows()>0){
			$result = $q->row();
		}
		return (($result->read_only=="true")?"sample":"uploaded")."/".$result->file_name;
	}

	function get_track($track_id,$include_extra=true){
		$this->db->where('track_id',$track_id);
		$q = $this->db->get('tracks');
		$result=false;
		if($q->num_rows()>0){
			$result = $q->row();
			if($include_extra){
				$result->track_location = base_url( "tracks/".(($result->read_only=="true")?"sample":"uploaded")."/".$result->file_name );
				$result->artists = unserialize($result->artists);
				$result->artists_str = implode(",",$result->artists);
			}
		} else {
			return false;
		}
		return $result;
	}

	function get_tracks ( $rp=false,$page=false, $sortname="title", $sortorder="asc", $qtype=false, $query=false, $for_flexigrid=false) {

		if($page && $rp){
			//$this->db->limit($rp,($page-1)*$rp); // TODO: Fix this - it returns $rp number of rows...
			//$this->db->limit($rp);
		}
		
		if($sortname && $sortorder){
			$this->db->order_by($sortname,$sortorder);
		}
		
		if($qtype && $query){
			$this->db->where($qtype,$query);
		}
		$q = $this->db->get('tracks');
		
		if(!$for_flexigrid){
			return $q->result('tracks');
		} else {
			$q_total=$this->db->select("COUNT(*)");
			if($qtype && $query){
				$this->db->where($qtype,$query);
			}
			$q2=$this->db->get('tracks');
			$total=$q2->row_array();
			$total = $total[0];
			$row_array=array();
			foreach($q->result() as $row){
				$row->track_location = base_url( "tracks/".(($row->read_only==true)?"sample":"uploaded")."/".$row->file_name );
				$row->artists = unserialize($row->artists);
				$row->artists_str = implode(",",$row->artists);
				$row_array[] = array(
					'id'=>$row->track_id,
					'cell'=>$row
				);
			}
			
			$result = array(
				'page'=>$page,
				'total'=>$total,
				'rows'=>$row_array
			);
			return $result;
		}
	}

	public function get_unique_artists(){
		$this->db->select('artists');
		$q = $this->db->get('tracks');
		$count_list = array();
		
		$i=0;
		foreach($q->result() as $artist_list){
			$cur_temp_list = unserialize($artist_list->artists);
			foreach($cur_temp_list as $artist){
				$artist=trim($artist);
				if(isset($count_list[$artist])){
					$count_list[$artist]+=1;
				} else {
					$count_list[$artist]=1;
				}
			}
		}
		$composite_list = array();
		foreach($count_list as $key=>$value){
			if(!isset($composite_list[strtoupper($key)])){
				$composite_list[strtoupper($key)]=$key;
			} elseif ($count_list[$composite_list[strtoupper($key)]]<$value){
				$composite_list[strtoupper($key)]=$key;
			}
		}
		
		$final_list = array();
		foreach($composite_list as $key=>$value){
			$final_list[]=$value;
		}
		natcasesort($final_list);
		return $final_list;
	}

	function remove_track($track_id){
		$this->err_msg = false;
		if(!$this->is_track_writable($track_id)) {
			if($this->err_msg==false){
				$this->err_msg = "The track is not writable";
			}
			return false;
		}
		$this->db->where("track_id", $track_id);
		$this->db->update('mytable', array( "hidden" => 'true'));
		$result = ($this->db->affected_rows()>0);
		if($result ==false){
			$this->err_msg = "No tracks were removed.";
		}
		return $result;
	}

	function add_track($title, $artists, $file_name, $hidden = 'false', $read_only = 'false'){
		$this->err_msg = false;
		$track = (object) array("title" => $title, "artists" => serialize($artists), "file_name" => $file_name, "hidden" => $hidden, "read_only" => $read_only);
		$this->db->insert('tracks', $track);
	}

	function edit_track($track_id, $title, $artists) {
		$this->err_msg = false;
		if(!$this->is_track_writable($track_id)){
			if(!$this->err_msg){
				$this->err_msg = "Cannot edit a read-only track!";
			}
			return false;
		}
		if(trim($title)){
			//$track->title = $title;
			$this->db->set('title',$title);
		}
		
		if($artists){
			$final_artists = array();
			foreach($artists as $val){
				if(trim($val)){
					$final_artists[] = trim($val);
				}
			}
			$this->db->set('artists',serialize($final_artists));
		}
		$this->db->where("track_id", $track_id);
		$this->db->update('tracks');
		$result = ($this->db->affected_rows()>0);
		if($result == false){
			$this->err_msg = "No tracks were edited.";
		}
		return $result;
	}

	function is_track_writable($track_id){
		$track = $this->get_track($track_id);
		if(!$track){
			$this->err_msg = "Track not found.";
			return false;
		}
		return (strtolower($track->read_only)=="false");
	}

	function reset_database(){
		$this->db->empty_table('tracks');
		$data = array(
			array(
				'title'=>'Sample Beat',
				'artists'=>serialize(array('P Diddy', 'Enya')),
				'file_name'=>"sampleBeat.mp3",
				'hidden'=>'false',
				'read_only'=>'true'
			),
			array(
				'title'=>'Sample Beat No. 2',
				'artists'=>serialize(array('Weird Al Yanchovich','The Beatles')),
				'file_name'=>"sampleBeat2.mp3",
				'hidden'=>'false',
				'read_only'=>'true'
			),
			array(
				'title'=>'Sample Piano tune',
				'artists'=>serialize(array('Beethoven')),
				'file_name'=>"samplePiano.mp3",
				'hidden'=>'false',
				'read_only'=>'true'
			),
			array(
				'title'=>'Sample Vocals',
				'artists'=>serialize(array('Lil\' Wayne')),
				'file_name'=>"sampleVocals.mp3",
				'hidden'=>'false',
				'read_only'=>'true'
			)
		);
		$this->db->insert('tracks',$data[0]);
		$this->db->insert('tracks',$data[1]);
		$this->db->insert('tracks',$data[2]);
		$this->db->insert('tracks',$data[3]);
	}

}

